import logging
import re


class MyLog(logging.Logger):

    def __init__(self, file_path):
        super().__init__(__name__)
        logging.basicConfig(filename=file_path, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s',
                            encoding='utf8')
        self.file_path = file_path
        self.last_page_num = self.get_last_page_num()

    def get_last_page_num(self):
        with open(self.file_path, encoding='utf8') as file:
            text = file.read()
        ans = re.findall('(?<=成功爬取第)\d+(?=页)', text)
        if ans:
            n = max(list(map(int, ans)))
            if n:
                return n

        return 1

    def write_success_text(self):
        logging.info(f'成功爬取第{self.last_page_num + 1}页。')
        self.last_page_num += 1

    def write_info(self, text):
        logging.info(text)

    def write_debug(self, text):
        logging.info(text)


if __name__ == "__main__":
    a = MyLog("a.log")
    a.write_success_text()
    a.write_info()
    print(a.get_last_page_num())
